from.__init__ import BaseDao
import hashlib
from mainapp.dao import  BaseDao

class CardDao(BaseDao):
    # 查询user表所有数据
    def list(self):
        sql ="""
            select c.id,c.number,c.money,
                    u.name as username,
                    b.name as bankname
            from card c 
            JOIN user u on(u.id=c.user_id)
            JOIN tb_bank b on (b.id=c.tb_bank_id)
        """

        result = super().query(sql)
        return result


    def save(self, **data):
        # 外部验证数据的完整性
        data['password'] = hashlib.md5(data['password'].encode('utf-8')).hexdigest()
        return super().save('card',**data)

    def update(self,**data):
        if not data.get('id'):
            return False
        id = data.pop('id')
        if data.get('password'):
            data['password'] == hashlib.md5(data.get('password').encode('utf-8')).hexdigest()

        return super().update('card',id,**data)


